package jp.eisbahn.mgapi.api.voice;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.json.simple.JSONObject;

import jp.eisbahn.mgapi.api.Model;

/**
 * This class is the base class to create model classes for Voice API.
 * @author Yoichiro Tanaka
 */
public abstract class AbstractVoiceModel extends Model {

    /** Date format to parse the createAt property. */
    private static final String DATE_FORMAT = "EEE MMM dd HH:mm:ss Z yyyy";

    /** The user object. */
    private User user;

    /**
     * Initialize this instance.
     * @param original The original information.
     */
    public AbstractVoiceModel(final JSONObject original) {
        super(original);
        user = new User(getPropertyJSONObject("user"));
    }

    /**
     * Retrieve the ID.
     * @return The ID string.
     */
    public final String getId() {
        return getPropertyString("id");
    }

    /**
     * Retrieve the createAt property value.
     * @return The createAt property value.
     * @throws ParseException If the date string is invalid.
     */
    public final Date getCreatedAt() throws ParseException {
        String createdAt = getPropertyString("created_at");
        if (createdAt != null) {
            DateFormat format = new SimpleDateFormat(DATE_FORMAT, Locale.US);
            Date date = format.parse(createdAt);
            return date;
        } else {
            return null;
        }
    }

    /**
     * Retrieve the text value.
     * @return The text string.
     */
    public final String getText() {
        return getPropertyString("text");
    }

    /**
     * Retrieve the object to have information about the user.
     * @return The user object.
     */
    public final User getUser() {
        return user;
    }

}
